package cn.bbdx.qsh.baking.controller;

import cn.bbdx.qsh.baking.pojo.dto.CommentDTO;
import cn.bbdx.qsh.baking.pojo.entity.Comment;
import cn.bbdx.qsh.baking.response.JsonResult;
import cn.bbdx.qsh.baking.response.StatusCode;
import cn.bbdx.qsh.baking.security.CustomUserDetails;
import cn.bbdx.qsh.baking.service.CommentService;
import cn.bbdx.qsh.baking.service.ContentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/v1/comments/")
public class CommentController {
    @Autowired
    CommentService commentService;

    @Autowired
    ContentService contentService;

    @PostMapping("add-new")
    public JsonResult addNew(@RequestBody CommentDTO commentDTO, @AuthenticationPrincipal CustomUserDetails userDetails) {
        if (userDetails == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreateTime(new Date());
        commentService.insert(comment);
        //让评论+1
        contentService.updateCommentCountById(commentDTO.getContentId());
        return JsonResult.ok();
    }

    @GetMapping("{id}")
    public JsonResult select(@PathVariable Long id) {
        System.out.println("返回内容为：" + commentService.selectByContentId(id));
        return JsonResult.ok(commentService.selectByContentId(id));
    }
}
